<?php

namespace app\api\controller;

use app\api\model\Message;
use app\api\model\Oauth;
use app\api\model\OauthFocus;
use app\api\model\OauthOrder;
use app\api\model\TieBa;
use app\api\model\TieBaCollect;
use think\facade\Log;
class Mine extends Base
{
    /**
     * 我的
     * @return array
     */
    public function index()
    {
        $oauthInfo = $this->oauthInfo;

        $oauthId = $oauthInfo['id'];
        $tieCount = TieBa::getCountMine($oauthId);
        $collectCount = TieBaCollect::getCountMine($oauthId, TieBaCollect::TYPE_COLLECT);
        $focusCount = OauthFocus::getCountMine($oauthId);
        $shareCount = OauthOrder::getCountMine($oauthId);
        $unreadMsg = Message::unreadCount($oauthId);
        //Log::write(checkMiniprogram()?"1111111111":"0000000", 'userAgentxxxxxxxxxxxxxxxxxxxxxxxxxxx');
        return self::sendResult(self::$okCode, [
            'oauthId' => $oauthId,
            'nickname' => $oauthInfo['nickname'],
            'sex' => $oauthInfo['sex'],
            'area' => $oauthInfo['address'],
            'headImg' => $oauthInfo['head_img'],
            'signature' => $oauthInfo['signature'],
            'autoplay' => $oauthInfo['autoplay'],
            'tieCount' => $tieCount,
            'collectCount' => $collectCount,
            'focusCount' => $focusCount,
            'shareCount' => $shareCount,
            'unreadMsg' => $unreadMsg
        ]);
    }


    /**
     * 指纹列表
     */
    public function getFingerprint() {
        $oauthInfo = $this->oauthInfo;
        $oauthId = $oauthInfo['id'];

        $info  = Oauth::getFingerprint($oauthId);
    }


    /**
     * 添加指纹
     */
    public function addFingerprint() {
        $oauthInfo = $this->oauthInfo;
        $oauthId = $oauthInfo['id'];
        $fingerprint = input('fingerprint');
        if ($fingerprint == null) {
            return self::sendResult(-2,[],'指纹参数有误');
        }
        $info  = Oauth::addFingerprint($oauthId,$fingerprint);
        return $info ?  self::sendResult(0, [], '添加指纹成功'):self::sendResult(-2, [], '添加失败');
    }
    /**
     * 删除指纹
     */
    public function delFingerprint() {
        $oauthInfo = $this->oauthInfo;
        $oauthId = $oauthInfo['id'];
        $fingerprint = input('fingerprint');
        if ($fingerprint == null) {
            return self::sendResult(-2,[],'指纹参数有误');
        }
        $info  = Oauth::delFingerprint($oauthId,$fingerprint);
        return $info ?  self::sendResult(0, [], '删除指纹成功'):self::sendResult(-2, [], '删除指纹失败');
    }

    /**
     * 编辑个性签名
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function signature()
    {
        $content = input('content');
        if (empty($content)) {
            return self::sendResult(100, [], '内容不能为空');
        }

        $oauthId = $this->oauthInfo['id'];

        Oauth::signature($oauthId, $content);

        return self::sendResult(self::$okCode, [], '编辑成功');
    }

    /**
     * 编辑自动播放
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function autoplay()
    {
        $autoplay = input('autoplay') ? input('autoplay') : 0;

        $autoArr = Oauth::attributes('autoplay');
        if (!isset($autoArr[$autoplay])) {
            return self::sendResult(102);
        }

        $oauthId = $this->oauthInfo['id'];
        Oauth::autoplay($oauthId, $autoplay);

        return self::sendResult(self::$okCode, [], '修改成功');
    }
}